/*
 * JSFConsole - Lightweight JSF Application Monitor
 * Copyright (C) 2009  Grzegorz Bugaj
 * http://www.gregbugaj.com
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.gregbugaj.reflector.cfg;


/**
 * Default implementation of the NamingStrategy
 * Converts long.package.with.many.packages.ClassName to ...many.packages.ClassName
 * @author devil
 *
 */
public class DefaultNamingStrategy implements ClassNamingStrategy{

	private static final int MAX_PACKAGE_DOTS = 2;


	public String getName(final Class<?> clazz) {
		if(clazz==null){
			return "Null clazz name";
		}
		
		boolean hasEnoughDots=false;
		String className=clazz.getName();
		char[] cout=new char[className.length()];
		int dotCount=0;
		int total=0;
		for(int i=className.length()-1;i>0;i--){
			char ch=className.charAt(i);
			if(ch=='.'){
				dotCount++;
				if(dotCount>MAX_PACKAGE_DOTS){
					hasEnoughDots=true;
					break;
				}
			}
			cout[total]=ch;
			total++;
		}
		char dot='.';
		if(!hasEnoughDots){
			dot=' ';	
		}
		
		char[] converted=new char[total+3];
			converted[0]=dot;
			converted[1]=dot;
			converted[2]=dot;
		for(int i=0;i<total;i++){
			converted[total+2-i]=cout[i];
		}

		return String.valueOf(converted);
	}

}
